<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>天气预报</title>
</head>
<style>
    *{
    margin: 0;
    padding: 0;
}
body{
    background-color:rgb(23, 130, 165);
}

.card{
    width: 400px;
    height: 300px;
    background-color:paleturquoise;
    margin: 50px auto;
}
.mycard{
    height: 40px;
    background-color:pink;
    color: paleturquoise;
    
}
.mycard p{
    margin-left: 10px;
    font-size: 20px;
    font-weight: 900;
    line-height: 40px;
}
.search{
    text-align: center;
    margin-top: 10px;
}
.search input{
    width: 200px;
    height: 30px;
    border-radius: 20px;
    
}
.search button{
    width: 60px;
    height: 30px;
    border-radius: 20px;
    background-color: red;
    color: paleturquoise;
    border: none;
    margin-left: 20px;

}
.weatherloading{
    text-align: center;
    line-height: 40px;
}
.gitee a{
    padding: 5px;
    text-decoration: none;
    color:pink;
    font-size: larger;
}
</style>
<body>
    <div class="gitee">
        <a href="https://gitee.com/lijiawo/web/tree/master/web/JavaScript/%E5%A4%A9%E6%B0%94"></a>
    </div>
    <div class="card">
        <div class="mycard">
            <p>天气查询</p>
        </div>
    <div class="search">
        <input type="text" placeholder="请输入城市名称" class="search-bar">
        <button onclick="showalert()">搜索</button>
    </div>
    <div class="weatherloading">
        <div class="location">
            <p class="city">北京</p>
        </div>
        <div class="temperature">
            <h1 class="temperature-degree">24°C</h1>
            <div class="weather-description">大雨</div>
            <div class="humidity">湿度：70%</div>
            <div class="wind">风速：20公里/小时</div>
            </div>
        </div>
    </div>
    <script type="text/javascript" >
        var btn=document.querySelector('button');
        var txt=document.querySelector('input');
        
        btn.onclick=function(){


        var val=txt.value;
        var xhr=new XMLHttpRequest();
        xhr.open('get','https://restapi.amap.com/v3/weather/weatherInfo?key=3f6a51edf7357fc78b56484202b2a6c4&city='+val);
            
    if(val==''){
        alert('请输入城市名称');
    }
        xhr.onreadystatechange=function(){
    if(xhr.readyState==4&&xhr.status==200){
        console.log(xhr.responseText);
        var responseObj = JSON.parse(xhr.responseText);
        var city = responseObj.lives[0].city;
        console.log('城市名称：', city);
        document.querySelector(".city").innerHTML = city;
        var temperature = responseObj.lives[0].temperature;
        document.querySelector(".temperature-degree").innerHTML=temperature+'°C';
        var weather = responseObj.lives[0].weather;
        document.querySelector(".weather-description").innerHTML=weather;
        var humidity = responseObj.lives[0].humidity;
        document.querySelector(".humidity").innerHTML='相对湿度：'+humidity+'%';
        var windpower = responseObj.lives[0].windpower;
        document.querySelector(".wind").innerHTML='风速：'+windpower+'公里/小时';
    }
    };
    xhr.send();
    };
    </script>
</body>
</html>